var app = getApp()
var Utils = require('../../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tablist: ["全部","待支付", "待接诊", "已到诊", "已结束", "已取消"],
    currentData: 0,
    list: [],
    page: 0,
    pagesize: 10,
    dingdanall: [],
    currentData: 0
  },
  //点击切换，滑块index赋值
  checkCurrent: function (e) {
    if (this.data.currentData === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentData: e.target.dataset.current
      })
    }
    this.setData({
      list: [],
      page: 0,
      currentData: e.target.dataset.current
    })
    this.getList(this.data.currentData);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var that = this;
    //that.getList(0);
  },
  getList(index) {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/zhuanjia.gh_order",
      data: {
        openid: wx.getStorageSync('openid'),
        zid: wx.getStorageSync('zid'),
        page: that.data.page,
        pagesize: that.data.pagesize,
        status: index
      },
      success: function (res) {
        console.log(res)
        var page = that.data.page + 1;
        that.setData({
          list: that.data.list.concat(res.data),
          page: page,
        })
      }
    })
  },
  //取消订单
  cancel(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    app.util.request({
      'url': "entry/wxapp/zhuanjia.gh_refundqx",
      data: {
        id: id
      },
      success: function (res) {
        that.setData({
          page: 0,
          list: []
        })
        that.getList(that.data.currentData)
      }
    })
  },
  pingjia(e) {
    console.log(e)
    var that = this
    var keywords = e.currentTarget.dataset.keywords
    var zid = e.currentTarget.dataset.zid
    var orders = e.currentTarget.dataset.orders
    var j_id = e.currentTarget.dataset.j_id
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/pingjia/pingjia?keywords=' + keywords + '&zid=' + zid + '&orders=' + orders + '&j_id=' + j_id,
    })
  },
  //去支付
  gopay(e) {
    var that = this
    var back_orser = e.currentTarget.dataset.back_orser
    var money = e.currentTarget.dataset.money
    var key_words = e.currentTarget.dataset.key_words
    var zid = e.currentTarget.dataset.zid
    var j_id = e.currentTarget.dataset.j_id;
    if (money == '0.00') {
      app.util.request({
        'url': 'entry/wxapp/yuyue.msgdh',
        data: {
          orders: back_orser,
          text: '挂号预约'
        },
        success: function (res) {
          console.log(res)

        }
      })
      wx.showToast({
        title: '预约成功！',
        icon: 'none',
        success: function () {
          setTimeout(function () {
            that.setData({
              list: [],
              page: 0
            })
            that.getList(that.data.currentData);
          }, 1900)
        }
      })
      return;

    } else {
      app.util.request({
        'url': 'entry/wxapp/yuyue.paywenzhen',
        header: {
          "Content-Type": "application/xml"
        },
        method: "GET",
        data: {
          openid: wx.getStorageSync('openid'),
          z_tw_money: money,
          orders: back_orser,
          key_words: key_words
        },
        success: function (res) {
          console.log(res);
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr, //字符串随机数
            'package': res.data.package,
            'signType': res.data.signType,
            'paySign': res.data.paySign,
            success: function (res) {
              console.log(res)

            },
            fail: function (res) {
              console.log(res)

            },
            'complete': function (res) {
              if (res.errMsg == 'requestPayment:ok') {
                app.util.request({
                  'url': 'entry/wxapp/yuyue.msgdh',
                  data: {
                    orders: orders,
                    text: '挂号预约'
                  },
                  success: function (res) {
                    console.log(res)

                  }
                })
                app.util.request({
                  'url': 'entry/wxapp/zhuanjia.updateghorder',
                  data: {
                    back_orser: orders,
                    text: '挂号预约'
                  },
                  success: function (res) {
                    console.log(res)

                  }
                })
                wx.showToast({
                  title: '预约成功！',
                  icon: 'none',
                  success: function () {
                    setTimeout(function () {
                      that.setData({
                        list: [],
                        page: 0
                      })
                      that.getList(that.data.currentData);
                    }, 1900)
                  }
                })
                return;
              }
            }
          })
        }
      })
    }
  },
  //去退款
  refund(e) {
    var that = this
    var id = e.currentTarget.dataset.id;
    app.util.request({
      'url': "entry/wxapp/zhuanjia.gh_refund",
      data: {
        id: id
      },
      success: function (res) {
        that.setData({
          list: [],
          page: 0
        })
        that.getList(that.data.currentData);
      }
    })
  },
  //取消
  refund_qx(e) {
    var that = this
    var id = e.currentTarget.dataset.id;
    app.util.request({
      'url': "entry/wxapp/zhuanjia.gh_refundqx",
      data: {
        id: id
      },
      success: function (res) {
        that.setData({
          list: [],
          page: 0
        })
        that.getList(that.data.currentData);
      }
    })
  },
  //完成
  finish() {},
  //删除
  deleteitem(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    app.util.request({
      'url': "entry/wxapp/zhuanjia.gh_del",
      data: {
        id: id
      },
      success: function (res) {
        that.setData({
          page: 0,
          list: []
        })
        that.getList(that.data.currentData)
      }
    })
  },
  //详情
  lookdetail: function (e) {
    var id = e.currentTarget.dataset.id;
    // wx.navigateTo({
    //   url: '/hyb_yl/backstageServices/pages/tijiandetail/tijiandetail?id=' + id,
    // })
    var that = this
    console.log(e)
    var list = e.currentTarget.dataset
    var orders = e.currentTarget.dataset.orders
    var j_id = e.currentTarget.dataset.j_id
    var key_words = that.data.key_words
    console.log(key_words)
    var zid = e.currentTarget.dataset.zid
    var ifpay = e.currentTarget.dataset.ifpay
    var money = e.currentTarget.dataset.money
    var overtime = e.currentTarget.dataset.overtime
    var id = e.currentTarget.dataset.id
    var currentData = that.data.currentData
    var ifgb = e.currentTarget.dataset.ifgb
    wx.setStorage({
      key: 'userinfotype',
      data: 'user',
    })
    let userInfo = wx.getStorageSync('userInfo')
    console.log(userInfo)
    let bl_id = e.currentTarget.dataset.bl_id
    wx.setStorage({
      key: 'userInfo',
      data: userInfo,
    })
    let z_thumbs = e.currentTarget.dataset.z_thumbs //医生头像
    wx.setStorage({
      key: 'z_thumbs',
      data: z_thumbs,
    })
    let sate = 0
    wx.setStorageSync('sate', sate) //证明是用户发送的信息
    wx.setStorageSync('docroom', your)
    var my = e.currentTarget.dataset.myroom; //自己
    var your = e.currentTarget.dataset.docroom; //医生
    var dex = e.currentTarget.dataset.dex;
    wx.setStorageSync('docroom', your)
    var nameList = {
      myName: my,
      your: your,
      bl_id: bl_id
    };
    if (ifpay == 5) {
      wx.showToast({
        title: '退款申请中无法查看详情',
        icon: 'none'
      })
      return false;
    }
    if (ifpay == 6) {
      wx.showToast({
        title: '已退款',
        icon: 'none'
      })
      return false;
    }

    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/postoperative_ordercont/postoperative_ordercont?txt=yes&zid=' + zid + '&back_orser=' + orders + '&key_words=yuanchengguahao' + '&j_id=' + j_id + '&ifpay=' + ifpay + '&money=' + money + '&id=' + id,
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var currentData = that.data.currentData
    console.log(currentData)
    app.util.request({
      'url': "entry/wxapp/zhuanjia.gh_order",
      data: {
        openid: wx.getStorageSync('openid'),
        zid: wx.getStorageSync('zid'),
        page: that.data.page,
        pagesize: that.data.pagesize,
        status: currentData
      },
      success: function (res) {
        console.log(res)
        var page = that.data.page + 1;
        that.setData({
          list: that.data.list.concat(res.data),
          page: page,
        })
      }
    })
    that.setData({
      currentData: currentData
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh: function () {

  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList(this.data.currentData)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})